//
//  BarView.m
//  滑动切换视图控制器
//
//  Created by Hubery on 15/8/28.
//  Copyright (c) 2015年 ifly. All rights reserved.
//

#import "BarView.h"

@interface BarView()

@property (nonatomic, strong) UIView *selectedBarView;
@property(nonatomic,strong) UIButton *btn1;
@property(nonatomic,strong) UIButton *btn2;
@property(nonatomic,strong) UIView *lineView;
@end

@implementation BarView


- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self addObserver];
        [self addBlackLine];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addObserver];
        [self addBlackLine];
    }
    return self;
}

-(void)addObserver{
    [self addObserver:self
           forKeyPath:@"page"
              options:NSKeyValueObservingOptionNew|
     NSKeyValueObservingOptionOld
              context:nil];
    [self addBtn];
}

-(void)addBlackLine{
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = [UIColor darkGrayColor];
    self.lineView.alpha = 0.28;
   [self addSubview:self.lineView];
}

-(void)addBtn{
    self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.btn1.tag = UserTypeManage;
    [self.btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.btn1 setTitleColor:MAINCOLOR forState:UIControlStateSelected];
    [self.btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.btn1.titleLabel.font = [UIFont systemFontOfSize:16];
    
    self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.btn2.tag = UserTypeCorp;
    [self.btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.btn2 setTitleColor:MAINCOLOR forState:UIControlStateSelected];
    [self.btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.btn2.titleLabel.font = [UIFont systemFontOfSize:16];
    
    [self addSubview:self.btn1];
    [self addSubview:self.btn2];
    
    
    self.selectedBarView = [[UIView alloc] init];
    self.selectedBarView.backgroundColor = MAINCOLOR;
    [self addSubview:self.selectedBarView];

}

- (void)layoutSubviews{
    [self.btn1 setFrame:CGRectMake(0, 0, self.frame.size.width/2, self.frame.size.height)];
    [self.btn2 setFrame:CGRectMake(self.frame.size.width/2, 0, self.frame.size.width/2, self.frame.size.height)];
    self.selectedBarView.frame = CGRectMake(0,self.frame.size.height - 2.4,self.frame.size.width/2,2.4);
    self.btn1.selected = YES;
    
    self.lineView.frame = CGRectMake(0, self.height - 1, self.width, 1);
}

-(void)btnClick:(UIButton *)sender
{
    [self.barCollectionDelegate barCollectionView:self didSelectionItemAtIndex:sender.tag];
}

- (void)setChildViewControllerTitles:(NSArray *)childViewControllerTitles
{
    _childViewControllerTitles = childViewControllerTitles;
    [self.btn1 setTitle:_childViewControllerTitles[0] forState:UIControlStateNormal];
    [self.btn2 setTitle:_childViewControllerTitles[1] forState:UIControlStateNormal];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSInteger new = [change[@"new"] integerValue];
    NSInteger old = [change[@"old"] integerValue];
    if (new != old) {
        CGPoint center = self.selectedBarView.center;
        if (self.selectedBarView.center.x == self.btn1.center.x) {
            center.x = self.btn2.center.x;
        }
        if (self.selectedBarView.center.x == self.btn2.center.x) {
            center.x = self.btn1.center.x;
        }
        
        [UIView animateWithDuration:.25
                              delay:.0f
             usingSpringWithDamping:.7
              initialSpringVelocity:0.f
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.selectedBarView.center = center;
                         } completion:nil];
        [self btnStateChange];//改变按钮选中状态
    }
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"page"];
}


-(void)btnStateChange
{
    if (self.selectedBarView.center.x == self.btn1.center.x) {
        self.btn1.selected = YES;
        self.btn2.selected = NO;
    }
    if (self.selectedBarView.center.x == self.btn2.center.x) {
        self.btn1.selected = NO;
        self.btn2.selected = YES;
    }
}

@end
